cmake_minimum_required(VERSION 3.5)

# ##############################################################################
# CMake Project ##
# ##############################################################################

# include useful macros
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/util.cmake)

# read metadata from package.xml and setup project
xict_common_metadata()

# set project name and version
project(
    ${extracted_name}
    VERSION ${extracted_version}
    LANGUAGES C CXX)

# =========#
# Settings #
# =========#

# introduce default building settings
xict_common_settings()

set(EXTRA_DEPS "")
set(EXTRA_INCLUDES "")

option(ENABLE_YALK "Whether to enable YALK license management." OFF)

if(ENABLE_YALK)
    add_definitions(-DENABLE_YALK)
    include(${PROJECT_SOURCE_DIR}/../../cmake/yalk.cmake)
    list(APPEND EXTRA_DEPS ${YALK_LIB})
    list(APPEND EXTRA_INCLUDES ${YALK_INCLUDE_DIR})
endif()

# =============#
# Dependencies #
# =============#

# ----------------------#
# Third-party libraries #
# ----------------------#
find_package(Threads REQUIRED)
find_package(OpenCV REQUIRED)
find_package(Catch2 REQUIRED)
find_package(fmt REQUIRED)
find_package(Boost REQUIRED COMPONENTS system filesystem)
find_package(CUDA REQUIRED)
find_package(yaml-cpp REQUIRED)
find_package(absl REQUIRED)

set(CMAKE_CUDA_COMPILER /usr/local/cuda/bin/nvcc)
set(CMAKE_CUDA_ARCHITECTURES 61;75;86;89)
enable_language(CUDA)

include_directories(${CUDA_INCLUDE_DIRS})
link_directories(${CUDA_LIBRARIES})

# ##############################################################################
# Build ##
# ##############################################################################

# ==============#
# Build targets #
# ==============#
# 包含处理generate_export_header的模块
include(GenerateExportHeader)

# ---------------#
# Library target #
# ---------------#
include_directories(${PROJECT_SOURCE_DIR}/include)

# file(GLOB_RECURSE PLUGIN_SRCS
# ${PROJECT_SOURCE_DIR}/src/xict_common/perception/plugin/*.cu)
# common library
# xict_common_library(
# NAME
# myplugins
# SRCS
# ${PLUGIN_SRCS}
# INCLUDES
# ${EXTRA_INCLUDES}
# DEPS
# nvinfer
# cudart
# ${EXTRA_DEPS})
# Find all source files
file(GLOB_RECURSE src_files
    ${PROJECT_SOURCE_DIR}/src/*/*.cc
    ${PROJECT_SOURCE_DIR}/src/*/*.cu
)

xict_common_library(
    NAME
    ${PROJECT_NAME}
    SRCS
    ${src_files}
    INCLUDES
    ${OpenCV_INCLUDE_DIRS}
    ${EXTRA_INCLUDES}
    DEPS
    dl
    Threads::Threads
    fmt::fmt
    absl::flags
    absl::flags_parse
    ${OpenCV_LIBS}
    nvinfer
    cudart
    ${YAML_CPP_LIBRARIES}
    ${Boost_LIBRARIES}
    ${Boost_FILESYSTEM_LIBRARY}
    ${EXTRA_DEPS})

# copy the directory assets to ${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME}
install(DIRECTORY ${PROJECT_SOURCE_DIR}/../../assets
    DESTINATION ${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME})

# ------------------#
# Executable targets #
# ------------------#
xict_common_apps()

# -------------#
# Test targets #
# -------------#
include(CTest)

# enable_testing()
# xict_common_tests("xict_common")
